<?php
namespace app\controller\admin;

use support\Request;
use app\service\AdminService;
use app\support\Response;
use hg\apidoc\annotation as Apidoc;

/**
 * @Apidoc\Title("Admin管理")
 * @Apidoc\Group("admin")
 */
class AdminController
{
    protected $service;

    public function __construct()
    {
        $this->service = new AdminService;
    }

    /**
     * @Apidoc\Title("获取Admin列表")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/Admin/index")
     * @Apidoc\Param("page", type="int", default=1, desc="页码")
     * @Apidoc\Param("page_size", type="int", default=15, desc="每页数量")
     * @Apidoc\Returned("data", type="object", desc="分页数据", children={
     *   @Apidoc\Returned("items", type="array", desc="数据列表", children={
     *     @Apidoc\Returned("id", type="int", desc="ID"),
     *     @Apidoc\Returned("created_at", type="string", desc="创建时间"),
     *     @Apidoc\Returned("updated_at", type="string", desc="更新时间")
     *   }),
     *   @Apidoc\Returned("total", type="int", desc="总数"),
     *   @Apidoc\Returned("page", type="int", desc="当前页"),
     *   @Apidoc\Returned("page_size", type="int", desc="每页数量")
     * })
     */
    public function index(Request $request)
    {
        try {
            $params = $request->all();
            $list = $this->service->list($params);
            return Response::paginate($list);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("创建Admin")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/admin/Admin/store")
     * @Apidoc\ParamType("json")
     * @Apidoc\Param("name", type="string", require=true, desc="名称")
     * @Apidoc\Param("status", type="int", default=1, desc="状态")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *   @Apidoc\Returned("id", type="int", desc="ID")
     * })
     */
    public function store(Request $request)
    {
        try {
            $data = $request->all();
            $result = $this->service->create($data);
            return Response::success($result, '创建成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("更新Admin")
     * @Apidoc\Method("PUT")
     * @Apidoc\Url("/admin/Admin/update/{id}")
     * @Apidoc\ParamType("json")
     * @Apidoc\Param("id", type="int", require=true, desc="ID")
     * @Apidoc\Param("name", type="string", desc="名称")
     * @Apidoc\Param("status", type="int", desc="状态")
     * @Apidoc\Returned("data", type="object", desc="返回数据", children={
     *   @Apidoc\Returned("id", type="int", desc="ID")
     * })
     */
    public function update(Request $request, $id)
    {
        try {
            $data = $request->all();
            $result = $this->service->update($id, $data);
            return Response::success($result, '更新成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("删除Admin")
     * @Apidoc\Method("DELETE")
     * @Apidoc\Url("/admin/Admin/destroy/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="ID")
     */
    public function destroy($id)
    {
        try {
            $result = $this->service->delete($id);
            return Response::success($result, '删除成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("获取Admin详情")
     * @Apidoc\Method("GET")
     * @Apidoc\Url("/admin/Admin/show/{id}")
     * @Apidoc\Param("id", type="int", require=true, desc="ID")
     * @Apidoc\Returned("data", type="object", desc="详情数据", children={
     *   @Apidoc\Returned("id", type="int", desc="ID"),
     *   @Apidoc\Returned("created_at", type="string", desc="创建时间"),
     *   @Apidoc\Returned("updated_at", type="string", desc="更新时间")
     * })
     */
    public function show($id)
    {
        try {
            $result = $this->service->find($id);
            return Response::success($result);
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }

    /**
     * @Apidoc\Title("批量删除Admin")
     * @Apidoc\Method("POST")
     * @Apidoc\Url("/admin/Admin/batchDestroy")
     * @Apidoc\ParamType("json")
     * @Apidoc\Param("ids", type="array", require=true, desc="ID数组")
     */
    public function batchDestroy(Request $request)
    {
        try {
            $ids = $request->post('ids');
            if (empty($ids)) {
                return Response::error('请选择要删除的记录');
            }
            $result = $this->service->batchDelete($ids);
            return Response::success($result, '删除成功');
        } catch (\Exception $e) {
            return Response::error($e->getMessage());
        }
    }
} 